package org.slf4jx.core;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jjliu
 * @date 2023/1/15
 */
public class Slf4jxUtils {

    /**
     * 解析参数
     *
     * @param configstr
     * @return
     */
    public static List<Map<String, Object>> setSpecifyLevelConfigList(String configstr) {
        List<Map<String, Object>> configList = new ArrayList<>();
        if (Slf4jxUtils.isBlank(configstr)) {
            return configList;
        }
        configList = Arrays.stream(configstr.split("[,，]"))
                .filter(x -> isNotBlank(x))
                .map(x -> x.trim().split("="))
                .filter(arr -> arr.length == 2)
                .map(arr -> {
                    String name = Optional.ofNullable(arr[0]).filter(x -> isNotBlank(x)).orElse(Slf4jxCons.SLF4JX_MDC_ALL);
                    Slf4jxLevelEnum slf4jxLevelEnum = Slf4jxLevelEnum.of(arr[1]);
                    if (slf4jxLevelEnum == null) {
                        return null;
                    }
                    return new HashMap<String, Object>() {{
                        put(Slf4jxCons.SLF4JX_MDC_NAME, name);
                        put(Slf4jxCons.SLF4JX_MDC_VALUE, slf4jxLevelEnum);
                    }};
                })
                .filter(map -> map != null)
                .collect(Collectors.toList());
        return configList;
    }


    public static boolean isBlank(String str) {
        if (str == null || str.trim().length() == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isEmpty(Collection<?> list) {
        if (list == null || list.size() == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(Collection<?> list) {
        return !isEmpty(list);
    }

    /**
     * 类+函数标识
     * @param method
     * @return
     */
    public static String getSignature(Method method) {
        return method.getName() + "("
                + parametersAsString(method) + ")";
    }

    /**
     * 只有函数标识
     * @param method
     * @return
     */
    public static String parametersAsString(Method method) {
        return parametersAsString(method, false);
    }

    public static String parametersAsString(Method method, boolean longTypeNames) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 0) {
            return "";
        }
        StringBuilder paramString = new StringBuilder();
        paramString.append(longTypeNames ? parameterTypes[0].getName()
                : parameterTypes[0].getSimpleName());
        for (int i = 1; i < parameterTypes.length; i++) {
            paramString.append(",").append(
                    longTypeNames ? parameterTypes[i].getName()
                            : parameterTypes[i].getSimpleName());
        }
        return paramString.toString();
    }

}
